
//
//  HTStatusPictrueView.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/22.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit



// 每一个条目之间的间距
let HMStatusPictureItemMargin: CGFloat = 5
// 每一个条目的宽高 (collectionView item 的宽高，尽量设置成整数)
let HMStatusPictureItemWH: CGFloat = {
    // 转成Int
    let intW = Int(( UIScreen.mainScreen().bounds.size.width - 2 * HMStatusCellMargin - 2 * HMStatusPictureItemMargin) / 3)
    // 转成 Float
    let floatW = CGFloat(intW)
    return floatW
}()
class HTStatusPictrueView: UICollectionView {

    let statusIdent : String = "statusIdent"

//    var pic_urls : [HTStatusPhotoInfo]? {
//        didSet{
//            lable.text = "\(pic_urls?.count ?? 0)"
//
//            reloadData()
//        }
//    }
    
    var dataInfo : (pic_urls : [HTStatusPhotoInfo], size : CGSize)?{
    
        didSet{
        
            if dataInfo!.pic_urls.count == 1{
                let  layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = dataInfo?.size ?? CGSizeZero
            }else{
            
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
            }
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(dataInfo?.size ?? CGSizeZero)
            }
            
            reloadData()
        }
        
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
//        backgroundColor = UIColor.yellowColor()
        
        registerClass(HTStatusPictrueViewCell.self, forCellWithReuseIdentifier: statusIdent)
        
        //设置每条之间的间距
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
        layout.minimumInteritemSpacing = HMStatusPictureItemMargin
        layout.minimumLineSpacing = HMStatusPictureItemMargin
        
        self.dataSource = self
        
        addSubview(lable)
        

        
        lable.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        
    }

    private lazy var lable : UILabel = {
        
        var lable = UILabel()
        lable.textColor = UIColor.blackColor()
        //lable.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 2 * HTstatusCellMargin
        lable.font = UIFont.systemFontOfSize(18)

        return lable
    }()

}

extension HTStatusPictrueView : UICollectionViewDataSource {

    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataInfo?.pic_urls.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(statusIdent, forIndexPath: indexPath) as! HTStatusPictrueViewCell
        
        cell.backgroundColor = UIColor.whiteColor()
        
        cell.photoInfo = dataInfo!.pic_urls[indexPath.item]
        
        return cell
    }
    
    
}

class HTStatusPictrueViewCell : UICollectionViewCell {
    
    
    var photoInfo : HTStatusPhotoInfo? {
        didSet{
            
            imageView.sd_setImageWithURL(NSURL(string: photoInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
        }
        
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func   setupUI(){
    
        contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView)
        }
    }
    
    private lazy var imageView : UIImageView = {
    
        let imageView : UIImageView  = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        imageView.contentMode = .ScaleAspectFill
        
        imageView.clipsToBounds = true
        
        return imageView
    }()
    
    
}


